package zrar.util;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

import zrar.entity.AnswerPeople;
import zrar.entity.Prize;

public class SystemUtil {

	private static List<AnswerPeople> peoples = new ArrayList<AnswerPeople>();
	private static List<Prize> prizes = new ArrayList<Prize>();

	private static List<String> resPrizes= new ArrayList<String>();
	
	private static Prize curPrize;//当前 正在抽的 奖
	private static int totalLabelNum = 0;// 在displayPanel 中 JLabel的总数量	
	private static int viewLabelNum = 0;// 在displayPanel 中需要显示抽奖名字的 JLabel的总数量		
	private static boolean resultSave=true;//结果是否保存
	private static boolean readystatu=false;
	
	
	public static String backgroud_image;//背景图片
	public static String result_path;//结果保存路径
	public static int speed;//滚动速度
	public static int scroll;//结果显示速度
	
	static{
		Properties props=new Properties();
		try {
			InputStreamReader in=new InputStreamReader(new FileInputStream("award.properties"),"UTF-8");
			props.load(in);
			in.close();
			backgroud_image = props.getProperty("背景图片");
			result_path = props.getProperty("保存路径");
			speed = Integer.parseInt(props.getProperty("滚动速度"));
			scroll = Integer.parseInt(props.getProperty("结果显示速度"));
		}catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void showInCenter(JFrame frame) {
		// 显示位置
		int HEIGHT = frame.getHeight();
		int WIDTH = frame.getWidth();
		Toolkit kit = Toolkit.getDefaultToolkit();// 设置顶层容器框架为居中
		Dimension screenSize = kit.getScreenSize();
		int width = screenSize.width;
		int height = screenSize.height;
		
		if(HEIGHT>height){
			height = HEIGHT;
		}
		if(WIDTH>width){
			width=WIDTH;
		}
		
		int x = (width - WIDTH) / 2;
		int y = (height - HEIGHT) / 2;
		frame.setLocation(x, y);
	}
	
	public static void close_Action() {

		int option = JOptionPane.showConfirmDialog(null, "是否退出系统(请确认结果已保存)",
				"提示 ", JOptionPane.YES_NO_OPTION);
		if (option == JOptionPane.YES_OPTION) {
			System.exit(0);
		} else if (option == JOptionPane.NO_OPTION) {
			return;
		}

	}

	public static ImageIcon createImageIcon(String path) {
		java.net.URL imgURL = SystemUtil.class.getClassLoader().getResource("zrar/image/" + path);
		if (imgURL != null) {
			return new ImageIcon(imgURL);
		} else {
			System.err.println("Couldn't find file: " + path);
			return null;
		}
	}

	public static boolean isInteger(String sum) {
		String num_regex = "^[0-9]+$";
		return sum.matches(num_regex);
	}

	// 验证文件扩展名 如："txt"
	public static boolean checkFileExtendName(String filename, String patten) {
		String fname = filename;

		int index = fname.lastIndexOf(".");

		String temp = fname.substring(index + 1, fname.length());

		return patten.equals(temp);
	}

	public static String getNowTime() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static List<AnswerPeople> getPeoplelist() {
		return peoples;
	}

	public static void setPeoplelist(List<AnswerPeople> peoplelist) {
		peoples = peoplelist;
	}

	public static List<Prize> getPrizelist() {
		return prizes;
	}

	public static void setPrizelist(List<Prize> prizelist) {
		prizes = prizelist;
	}

	public static Prize getCurPrize() {
		return curPrize;
	}

	public static void setCurPrize(Prize prize) {
		SystemUtil.curPrize = prize;
	}

	public static Prize findPrizeByName(String prizeName) {

		for (Prize prize : prizes) {
			if (prizeName.equals(prize.getPname())) {
				return prize;
			}
		}

		return null;
	}

	public static int getViewLabelNum() {
		return viewLabelNum;
	}

	public static void setViewLabelNum(int viewLabelNum) {
		SystemUtil.viewLabelNum = viewLabelNum;
	}

	public static int getTotalLabelNum() {
		return totalLabelNum;
	}

	public static void setTotalLabelNum(int totalLabelNum) {
		SystemUtil.totalLabelNum = totalLabelNum;
	}

	public static boolean isResultSave() {
		return resultSave;
	}

	public static void setResultSave(boolean resultSave) {
		SystemUtil.resultSave = resultSave;
	}

	public static boolean getReadystatu() {
		return readystatu;
	}

	public static void setReadystatu(boolean readystatu) {
		SystemUtil.readystatu = readystatu;
	}

	public static List<String> getResultPrizes() {
		return resPrizes;
	}

	public static void setResultPrizes(List<String> resPrizes) {
		SystemUtil.resPrizes = resPrizes;
	}
}
